package com.tg.app.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.Window;
import android.widget.ListView;
import android.widget.Toast;

import androidx.annotation.Nullable;

import com.appbase.custom.base.SettingData;
import com.base.BaseActivity;
import com.demo.view.LoadingDialog;
import com.tg.app.R;
import com.tg.app.TGSdk;
import com.tg.app.activity.device.DeviceListActivity;
import com.tg.app.activity.device.DeviceSettingsActivity;
import com.tg.app.adapter.SettingAdapter;
import com.tg.app.camera.AVIOCTRLDEFs;
import com.tg.app.camera.Camera;
import com.tange.module.camera.hub.CameraHub;
import com.tg.data.bean.DeviceSettingsInfo;
import com.tg.data.media.OnICameraListener;

import java.util.ArrayList;

public class AntiActivity extends BaseActivity implements OnICameraListener {
    private final ArrayList<SettingData> datas = new ArrayList<>();
    private SettingAdapter adapter;
    private Camera camera;
    private DeviceSettingsInfo info;
    private int clickPosition = 0;
    LoadingDialog mLoadingDialog;
    Handler mHandler = new Handler();
    Boolean mIsSetSuccess = false;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);//remove title bar  即隐藏标题栏
        setContentView(R.layout.activity_record_check);
        hideActionBar();

        info = getIntent().getParcelableExtra(DeviceSettingsActivity.EXT_DEVICE_INFO);
        if (info != null) {
            camera = CameraHub.getInstance().getCamera(info.uuid);
        }

        initView();

    }

    @Override
    public boolean shouldSetStatusBarColor() {
        return true;
    }

    public void initView() {
        ListView listView = findViewById(R.id.record_check_list);
        final SettingData data = new SettingData(getString(R.string.frequency_60), 0);
        SettingData data1 = new SettingData(getString(R.string.frequency_50), 0);
        datas.add(data);
        datas.add(data1);
        if (info != null && info.envMode == 1) {
            data.setFlag(1);
        } else {
            data1.setFlag(1);
        }
        adapter = new SettingAdapter(datas, this);
        listView.setAdapter(adapter);
        mLoadingDialog = new LoadingDialog(this);
        mLoadingDialog.setMsg(getString(R.string.loading));
        listView.setOnItemClickListener((parent, view, position, id) -> {
            if (datas.get(position).getFlag() == 0) {
                clickPosition = position;
                startSendui();
                if (position == 0) {
                    if (camera != null) {
                        camera.sendIOCtrl(AVIOCTRLDEFs.IOTYPE_USER_IPCAM_SET_ENVIRONMENT_REQ, AVIOCTRLDEFs.SMsgAVIoctrlSetEnvironmentReq.parseContent(0, (byte) AVIOCTRLDEFs.AVIOCTRL_ENVIRONMENT_INDOOR_60HZ));
                    }
                } else {
                    if (camera != null) {
                        camera.sendIOCtrl(AVIOCTRLDEFs.IOTYPE_USER_IPCAM_SET_ENVIRONMENT_REQ, AVIOCTRLDEFs.SMsgAVIoctrlSetEnvironmentReq.parseContent(0, (byte) AVIOCTRLDEFs.AVIOCTRL_ENVIRONMENT_INDOOR_50HZ));
                    }
                }
            }
        });

        modifyToolBar(getString(R.string.anti_flicker));
        findViewById(R.id.back_toolbar).setOnClickListener(v -> finish());
    }

    private void setFlag(int position) {
        for (int i = 0; i < datas.size(); i++) {
            if (i == position) {
                datas.get(i).setFlag(1);
            } else {
                datas.get(i).setFlag(0);
            }
        }
        adapter.notifyDataSetChanged();
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (camera != null) {
            camera.registerICameraListener(this);
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (camera != null) {
            camera.unregisterICameraListener(this);
        }
    }

    @Override
    public void receiveUpdateConnectStates(int state) {

    }

    @Override
    public void receiveIOCtrlData(int type, byte[] data) {
        if (type == AVIOCTRLDEFs.IOTYPE_USER_IPCAM_SET_ENVIRONMENT_RESP) {
            if (data[4] == 0) {
                runOnUiThread(() -> {
                    completeSend();
                    Toast.makeText(AntiActivity.this, R.string.modify_to_reboot, Toast.LENGTH_SHORT).show();
                    setFlag(clickPosition);
                    if (!TGSdk.getInstance().isThirdPartyApp()) {
                        Intent intent = new Intent(AntiActivity.this, DeviceListActivity.class);
                        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                        startActivity(intent);
                        finish();
                    }
                });

            }
        }
    }


    public void startSendui() {
        showLoading();
        mIsSetSuccess = false;
        mHandler.postDelayed(timeRunnable, 15000);
    }

    public void completeSend() {
        hideLoading();
        mIsSetSuccess = true;
        mHandler.removeCallbacks(timeRunnable);
    }

    Runnable timeRunnable = () -> {
        hideLoading();
        if (!mIsSetSuccess) {
            showToast(R.string.setting_fail);
        }
    };
}
